/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2010 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.components.game.notfinished.details;

import java.util.Calendar;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;

import es.sonxurxo.androidrunner.model.service.game.to.GameTO;
import es.sonxurxo.androidrunner.view.web.pages.game.find.details.notfinished.GameDetailsPage;
import es.sonxurxo.androidrunner.view.web.pages.game.live.WatchGamePage;
import es.sonxurxo.androidrunner.view.web.session.AndroidRunnerSession;

/**
 * @author "Xurxo Mendez Perez"
 *
 */
public class GameDetailsPanel extends Panel {

	private static final long serialVersionUID = 1132891939838452650L;

	public GameDetailsPanel(final String id, final GameTO gameTO) {
		super(id);
		add(new Label("city", gameTO.getCity()));
		add(new Label("pointsToWin", String.valueOf(gameTO.getPointsToWin())));
		add(new Label("maxPlayers", String.valueOf(gameTO.getMaxPlayers())));
		WebMarkupContainer holder = new WebMarkupContainer("currentPlayersHolder") {
			
			private static final long serialVersionUID = -8175092257950554797L;

			@Override
			public boolean isVisible() {
				Calendar now = Calendar.getInstance();
				return gameTO.getStartDate().before(now) && gameTO.getFinishDate().after(now);
			}
		};
		holder.add(new Label("currentPlayers", String.valueOf(gameTO.getCurrentPlayers())));
		add(holder);
		WebMarkupContainer currentlyActiveContainer = new WebMarkupContainer("currentlyActiveDiv") {
        	
        	private static final long serialVersionUID = 85149833378851638L;

			@Override
			public boolean isVisible() {
				
				Calendar now = Calendar.getInstance();
				return gameTO.getStartDate().before(now) && gameTO.getFinishDate().after(now);
			}
        };
        if (AndroidRunnerSession.get().getRoles().hasRole("ADMIN")) {
        	PageParameters linkParameters = new PageParameters();
            linkParameters.put("gameId", new Long(gameTO.getGameId()));
            currentlyActiveContainer.add(new BookmarkablePageLink("currentlyActiveLink", WatchGamePage.class, linkParameters)
            .add(new Label("currentlyActiveLinkText", getLocalizer().getString("gameDetails.watch", GameDetailsPanel.this))));
        }
        else {
        	PageParameters linkParameters = new PageParameters();
            linkParameters.put("gameId", new Long(gameTO.getGameId()));
            currentlyActiveContainer.add(new BookmarkablePageLink("currentlyActiveLink", GameDetailsPage.class, linkParameters) {

				private static final long serialVersionUID = 4395480853896770495L;

				@Override
				public boolean isEnabled() {
					return false;
				}
        	}.add(new Label("currentlyActiveLinkText", getLocalizer().getString("gameDetails.currentlyActive", GameDetailsPanel.this))));
        }
		add(currentlyActiveContainer);
	}
}
